const Koa = require("koa");
const Router = require("@koa/router");
const app = new Koa();
const path = require("path");
const serve = require("koa-static");
const koaBody = require("koa-bodyparser");
const cors = require("@koa/cors");
const UserRouter = require("./api/usercmd");
const FoodMenuRouter = require("./api/foodmenu");
const CustomerRouter = require("./api/getcustomer");
const OrderAndTableRouter = require("./api/orderandtable");
const ImageUploadRouter = require("./api/imageupload");
const OunceRouter = require("./api/ounceapi");
const SoupsRouter = require("./api/soupapi");
const EmployeeRouter = require("./api/employapi");
const {User} = require("./model/user/user");
const {Customer} = require("./model/customer/customer");
const {Menu} = require("./model/foodmenu/menu");
const {Ounce} = require("./model/foodmenu/ounce");
const {Soups} = require("./model/foodmenu/soups");
const {Order} = require("./model/orders/orders");
const {Table} = require("./model/orders/tables");
const {Employee} = require("./model/employee/employee");
class App {
  constructor() {
    //this.app = new Koa();
    this.PORT = process.env.PORT || 3000;
    // 允许所有源的跨域请求
    app.use(cors());
    // 将/uploads路径映射到存储图片的目录
    app.use(serve(path.join(__dirname, "./upload")));
    // 连接到数据库
    this.connectDatabase();

    // 初始化路由和中间件
    this.initializeMiddlewares();
    this.initializeRoutes();

    // 启动服务器
    this.startServer();
  }

  connectDatabase() {
    const {sequelize} = require("./model/config/sequelizeCommon");

    // 连接到数据库
    sequelize
      .authenticate()
      .then(() => {
        console.log("Connection has been established successfully.");
      })
      .catch((err) => {
        console.error("Unable to connect to the database:", err);
      });
  }

  initializeMiddlewares() {
    // 解析 JSON 请求体

    app.use(koaBody());
    // 将数据库模型添加到 ctx.state
    app.use(async (ctx, next) => {
      ctx.state = {User, Customer, Menu, Order, Table, Ounce, Soups, Employee};

      await next();
    });
  }

  initializeRoutes() {
    const router = new Router();
    //router.use(koaBody);
    router.use("/api/user", UserRouter.routes());
    router.use(UserRouter.allowedMethods());
    router.use("/api/menu", FoodMenuRouter.routes());
    router.use(FoodMenuRouter.allowedMethods());
    router.use("/api/customer", CustomerRouter.routes());
    router.use(CustomerRouter.allowedMethods());
    router.use("/api/order", OrderAndTableRouter.routes());
    router.use(OrderAndTableRouter.allowedMethods());
    router.use("/api/upload", ImageUploadRouter.routes());
    router.use(ImageUploadRouter.allowedMethods());
    router.use("/api/ounce", OunceRouter.routes());
    router.use(OunceRouter.allowedMethods());
    router.use("/api/soups", SoupsRouter.routes());
    router.use(SoupsRouter.allowedMethods());
    router.use("/api/employes", EmployeeRouter.routes());
    router.use(EmployeeRouter.allowedMethods());
    app.use(router.routes());
  }

  startServer() {
    // 启动服务器
    app.listen(this.PORT, () => {
      console.log(`Server is running on http://localhost:${this.PORT}`);
    });
  }
}

module.exports = App;
